package com.weizhi.server.util;

import lombok.SneakyThrows;
import org.springframework.beans.BeanUtils;

import java.util.ArrayList;
import java.util.List;

public class PropertyCopyUtil {
    @SneakyThrows
    public static <T> List<T> copyList(List source, Class<T> clazz){
        List<T> ts = new ArrayList<>();
        for (int i = 0; i < source.size(); i++) {
            T t = clazz.newInstance();
            BeanUtils.copyProperties(source.get(i), t);
            ts.add(t);
        }
        return ts;
    }

    @SneakyThrows
    public static <T> T copy(Object source, Class<T> clazz){
        if(source == null) return null;
        T t = clazz.newInstance();
        BeanUtils.copyProperties(source, t);
        return t;
    }
}
